package bma.common.c3p0;

import java.sql.Connection;
import java.util.concurrent.atomic.AtomicInteger;

import com.mchange.v2.c3p0.AbstractConnectionCustomizer;

public class TraceConnectionCustomizer extends AbstractConnectionCustomizer {

	final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(TraceConnectionCustomizer.class);

	private AtomicInteger counter = new AtomicInteger();

	@Override
	public void onCheckIn(Connection c, String parentDataSourceIdentityToken)
			throws Exception {
		int v = counter.decrementAndGet();
		if (log.isInfoEnabled()) {
			log.info("c3p0 checkIn - {}", v);
		}
		super.onCheckIn(c, parentDataSourceIdentityToken);
	}

	@Override
	public void onCheckOut(Connection c, String parentDataSourceIdentityToken)
			throws Exception {
		int v = counter.incrementAndGet();
		if (log.isInfoEnabled()) {
			log.info("c3p0 checkOut - {}", v);
		}
		super.onCheckOut(c, parentDataSourceIdentityToken);
	}
}
